package com.atshiyou.dao.impl;

import com.atshiyou.bean.Student;
import com.atshiyou.dao.StudentDao;
import com.atshiyou.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends DruidUtil implements StudentDao {
    List<Student> list = new ArrayList<>();
    @Override
    public List<Student> findAll() {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        String sql = "select * from student";
        try {
             statement = connection.prepareStatement(sql);
             resultSet = statement.executeQuery();
            while(resultSet.next()) {
                Student student = new Student();
                student.setStudentid(resultSet.getInt("studentid"));
                student.setGradeid(resultSet.getInt("gradeid"));
                student.setStuage(resultSet.getInt("stuage"));
                student.setStudentno(resultSet.getString("studentno"));
                student.setStuname(resultSet.getString("stuname"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,statement,resultSet);
        }
        return list;
    }

    @Override
    public boolean addStudent(Student student) {
        if(student == null){
            return false;
        }
        Connection connection = getConnection();
        String stuname = student.getStuname();
        String studentno = student.getStudentno();
        int stuage = student.getStuage();
        int gradeid = student.getGradeid();
        int studentid = student.getStudentid();
        PreparedStatement preparedStatement = null;
        int i = 0;
        try {
             preparedStatement = connection.prepareStatement("insert into student values(?,?,?,?,?)");
             preparedStatement.setInt(1,studentid);
             preparedStatement.setString(2,studentno);
             preparedStatement.setString(3,stuname);
             preparedStatement.setInt(4,stuage);
             preparedStatement.setInt(5,gradeid);

             i = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        if(i==0) {
            return false;
        }else if(i == 1) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteStudent(int studentid) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        int i = 0;
        try {
            preparedStatement = connection.prepareStatement("delete from student where studentid=?");
            preparedStatement.setInt(1,studentid);
            i = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(i==1){
            return true;
        }else
         return false;
    }

    @Override
    public boolean alterStudent(int stuid, Student student) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        int i = 0;
        try {
            preparedStatement = connection.prepareStatement("update student set studentno=?,stuname=?,stuage=?,gradeid=? where studentid=?");
            preparedStatement.setString(1,student.getStudentno());
            preparedStatement.setString(2,student.getStuname());
            preparedStatement.setInt(3,student.getStuage());
            preparedStatement.setInt(4,student.getGradeid());
            preparedStatement.setInt(5,stuid);
            i = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(i==1){
            return true;
        }else
            return false;

    }
}
